package com.eqxiu.ieditor.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
	
	@Value("${site.hostname}")
	private String hostname;
	@Value("${swagger.enabled}")
	private boolean enabled;
	
	@Bean
	public Docket adminApi() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("Intelligent Editor API")
				.enable(enabled).forCodeGeneration(true).pathMapping("/")
//				.host(hostname)
				.select().paths(paths()).apis(RequestHandlerSelectors.basePackage("com.eqxiu.ieditor")).build()
				.apiInfo(apiInfo()).useDefaultResponseMessages(false);
	}

	private Predicate<String> paths() {
		return Predicates.and(PathSelectors.regex("/.*"), Predicates.not(PathSelectors.regex("/error")));
	}

	private ApiInfo apiInfo() {
		Contact contact = new Contact("IE team", "", "");
		return new ApiInfoBuilder().title("Intelligent Editor API").license("Eqxiu 1.0")
				.contact(contact).version("1.0").build();
	}

//	private SecurityContext xBasicSecurityContext() {
//		return SecurityContext.builder()
//				.securityReferences(Lists.newArrayList(new SecurityReference("xBasic", new AuthorizationScope[0])))
//				.build();
//	}
//
//	private SecurityContext xAuthTokenSecurityContext() {
//		return SecurityContext.builder()
//				.securityReferences(Lists.newArrayList(new SecurityReference("xAuthToken", new AuthorizationScope[0])))
//				.build();
//	}

}
